import { ComponentWithStore } from "mobx-miniprogram-bindings";
import { userStore } from "../../store/userStore";
import { clearStorage } from "../../utils/storage";
import { getMajorNameByIdService } from "../../api/school";

ComponentWithStore({
  /**
   * 页面的初始数据
   */
  data: {
    majorname: "",
    gradename: "",
  },

  storeBindings: {
    store: userStore,
    fields: ["token", "userInfo", "isLogin"],
    actions: ["setToken", "setUserInfo", "setIsLogin"],
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      if (!this.data.isLogin) {
        this.setData({ showModal2: true });
      }
      if (this.data.userInfo.major !== null) this.setMajorname();
      if (this.data.userInfo.grade !== null) this.setGradename();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      // 每次页面显示时重新计算年级名称和专业名称，确保数据同步
      if (this.data.userInfo.grade !== null) this.setGradename();
      if (this.data.userInfo.major !== null) this.setMajorname();

      // 检查本地存储中的登录状态
      const token = wx.getStorageSync('token');
      const userInfo = wx.getStorageSync('userInfo');
      
      // 如果本地有token，确保store状态同步
      if (token) {
        // 更新store状态
        this.setToken(token);
        this.setIsLogin(true);
        if (userInfo) {
          this.setUserInfo(userInfo);
        }
        
        // 确保页面数据也同步更新
        this.setData({ 
          showModal2: false // 隐藏登录弹窗
        });
      } else {
        // 如果没有token，显示登录弹窗
        this.setData({ 
          showModal2: true 
        });
      }
    },

    /**
     * 根据数据项中的major数值，将其转换成majorname
     */
    setMajorname: async function () {
      try {
        const majorInfo = await getMajorNameByIdService(
          this.data.userInfo.major,
        );
        this.setData({
          majorname: majorInfo.data.majorName,
        });
      } catch (e) {
        console.error("获取专业名称失败:", e);
        if (this.data.userInfo.isLogin) {
          wx.showToast({
            title: "获取专业名称失败",
            icon: "none",
          });
        }
      }
    },

    /**
     * 根据数据项中的grade数值，将其转换成gradename
     */
    setGradename: function () {
      let gradename = "";
      switch (this.data.userInfo.grade) {
        case 1:
          gradename = "大一";
          break;
        case 2:
          gradename = "大二";
          break;
        case 3:
          gradename = "大三";
          break;
        case 4:
          gradename = "大四";
          break;
        case 5:
          gradename = "研究生";
          break;
        case 6:
          gradename = "博士";
          break;
        default:
          gradename = "----";
          console.error("grade name convertion error:\nunknown grade number");
          break;
      }
      this.setData({
        gradename,
      });
    },

    onCancel() {
      this.setData({
        showModal2: false,
        showOverlay: false,
      });
    },

    myRedact() {
      wx.navigateTo({
        url: "../myRedact/myRedact",
      });
    },

    //参与项目
    joinProject(){
      wx.navigateTo({
        url: '../joinProject/joinProject',
      })
    },
    myTeam() {
      wx.navigateTo({
        url: "../myTeam/myTeam",
      });
    },

    interested() {
      wx.navigateTo({
        url: "../interested/interested",
      });
    },

    servicePeople() {
      wx.navigateTo({
        url: "../servicePeople/servicePeople",
      });
    },

    aboutWe() {
      wx.navigateTo({
        url: "../aboutWe3/aboutWe3",
      });
    },

    commonSettings()
    {
      wx.navigateTo({
        url: "./commonSettings",
      });
    },

    schoolAccreditation() {
      wx.navigateTo({
        url: "../schoolAccreditation/schoolAccreditation",
      });
    },
  },
});
